package br.com.catalogodigital.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class FiltroLogin
 */
@WebFilter("/paginas/*")
public class FiltroLogin implements Filter {

	FilterConfig filterConfig;
	
    public FiltroLogin() {
        
    }
	public void init(FilterConfig fConfig) throws ServletException {
		this.filterConfig = fConfig;
	}
	public void destroy() {
		this.filterConfig = null;
	}

	
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse respo = (HttpServletResponse) response;
		
		HttpSession sesssion = req.getSession();
		
		if(sesssion.getAttribute("anunciante") != null){
			chain.doFilter(request, response);
			
		}else{
			respo.sendRedirect("../login.xhtml");
		}
 	}

	
	

}
